package com.office.meetingManage.meetroom.controller;

import com.office.meetingManage.meetroom.pojo.OaMeetroom;
import com.office.meetingManage.meetroom.pojo.otherPojo.MeetCountExcel;
import com.office.meetingManage.meetroom.service.MeetroomService;
import com.office.personalOffice.contacts.pojo.TbStaff;
import com.office.util.ExcelUtilXlsx;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Auther: 许月
 * @Date: 2022/10/27/9:26
 */
@RestController
@RequestMapping("/meetroom")
public class MeetroomController {

    @Autowired
    private MeetroomService meetroomService;


    /**
     * 会议室查询操作
     *
     * @param oaMeetroom
     * @param pageModel
     * @return
     */
    @RequestMapping("/list")
    public PageModel<OaMeetroom> getList(OaMeetroom oaMeetroom, PageModel<OaMeetroom> pageModel) {
        if ("".equals(pageModel.getPageNum())) {
            pageModel.setPageNum(1);
            pageModel.setPageSize(3);
        }
/*        System.out.println("***********************************************************");
        System.out.println("前端传来的状态"+oaMeetroom.getStatus());
        System.out.println(pageModel.getPageNum());
        System.out.println("***********************************************************");*/
        pageModel = meetroomService.getList(oaMeetroom, pageModel);
        return pageModel;
    }

    /**
     * 展示会议室信息
     *
     * @param mId
     * @return
     */
    @RequestMapping({"/show/{mId}", "/toupdate/{mId}"})
    public OaMeetroom getBymId(@PathVariable("mId") Integer mId) {
        /*        System.out.println(mId);*/
        OaMeetroom meetroom = meetroomService.getBymId(mId);
/*        System.out.println("***********************************************************");
        System.out.println("上传展示页面的信息："+meetroom);
        System.out.println("***********************************************************");*/
        return meetroom;
    }

    /**
     * 修改操作
     *
     * @param meetroom
     * @return
     */
    @RequestMapping("/update")
    public Boolean updateMeetRoom(@RequestBody OaMeetroom meetroom) {

        Boolean flag = false;
        if (meetroom != null && !"".equals(meetroom)) {
            flag = meetroomService.updateMeetRoom(meetroom);
        }
        return flag;
    }

    /**
     * 添加会议室操作
     *
     * @param meetroom
     * @return
     */
    @RequestMapping("/add")
    public Boolean addMeetRoom(@RequestBody OaMeetroom meetroom) {

        Boolean flag = false;
        if (meetroom != null && !"".equals(meetroom)) {
            flag = meetroomService.addMeetRoom(meetroom);
        }
        return flag;
    }

    /**
     * 删除操作
     *
     * @param mId
     * @return
     */
    @RequestMapping("/delete/{mId}")
    public Boolean deleteMeetRoom(@PathVariable("mId") Integer mId) {
        System.out.println("###############################################");
        System.out.println(mId);
        Boolean flag = false;
        if (mId != null && !"".equals(mId)) {
            flag = meetroomService.deleteMeetRoom(mId);
        }
        return flag;
    }


    /**
     * 判断下载是否有数据
     *
     * @param startMonth
     * @param nextMonth
     * @return
     */
    @RequestMapping("/estimate")
    public Boolean estimateData(String startMonth, String nextMonth) {
        List<MeetCountExcel> meetCountExcels = meetroomService.findMeetCount(startMonth, nextMonth);
        if (meetCountExcels.size() == 0) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * 会议室使用报表导出
     *
     * @param startMonth
     * @param nextMonth
     * @param response
     * @return
     */
    @RequestMapping("/download")
    public String excelData(String startMonth, String nextMonth, HttpServletResponse response) throws ParseException, IOException {

        System.out.println(startMonth);
        System.out.println(nextMonth);

        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startMonth);
        int month = date.getMonth() + 1;


        List<MeetCountExcel> meetCountExcels = meetroomService.findMeetCount(startMonth, nextMonth);
        if (meetCountExcels.size() == 0) {
            System.out.println(month + "月没有使用会议室！！！");
            response.sendRedirect("/meetingManage/meetroom/meetroomlist.html");

        } else {
            List<Object[]> meetExcelList = new ArrayList<>();
            for (MeetCountExcel meetCountExcel : meetCountExcels) {
                Object[] exceldata = new Object[3];
                exceldata[0] = meetCountExcel.getMeetRoomName();
                exceldata[1] = meetCountExcel.getMeetRoomPlace();
                exceldata[2] = meetCountExcel.getUseCount();
                meetExcelList.add(exceldata);
            }
            ExcelUtilXlsx excelUtilXlsx = new ExcelUtilXlsx();
            String title = month + "月会议室使用报表";
            String[] colHead = {"会议室名称", "会议室地址", "使用次数"};
            excelUtilXlsx.exportExcel(response, title, colHead, meetExcelList);
        }
        return null;
    }

    /**
     * 判断添加的会议室名称是否重复
     * @param meetRoomName
     * @return
     */
    @RequestMapping("/testMeetRoomName/{meetRoomName}")
    public Boolean testMeetRoomName(@PathVariable("meetRoomName") String meetRoomName) {
        Boolean flag = meetroomService.findByMeetRoomName(meetRoomName);

        System.out.println(flag);
        return flag;
    }



    /**
     * 判断添加的会议室地址是否重复
     * @param meetRoomPlace
     * @return
     */
    @RequestMapping("/testMeetRoomPlace/{meetRoomPlace}")
    public Boolean testMeetRoomPlace(@PathVariable("meetRoomPlace") String meetRoomPlace) {
        Boolean flag = meetroomService.findByMeetRoomPlace(meetRoomPlace);

        System.out.println(flag);
        return flag;
    }


}
